package com.jews.mongodb.support;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.AbstractAggregateRoot;

import java.time.Instant;

/**
 * @author JewsChen
 * @title: BaseJpaAggregate
 * @date 2022/11/28 23:15
 */
// 标注为@MappedSuperclass的类将不是一个完整的实体类，他将不会映射到数据库表，
// 但是他的属性都将映射到其子类的数据库字段中。
//@MappedSuperclass
@Data
public abstract class BaseJpaAggregate extends AbstractAggregateRoot<BaseJpaAggregate> {
    @Id
    @Setter(AccessLevel.PROTECTED)
//    @GeneratedValue(strategy = GenerationType.IDENTITY)
//    @Column(name = "id")
    private Long id;

    //    @Column(name = "created_at", nullable = false, updatable = false)
//    @Convert(converter = InstantLongConverter.class)
    @Setter(AccessLevel.PROTECTED)
    private Instant createdAt;

    //    @Column(name = "updated_at", nullable = false)
//    @Convert(converter = InstantLongConverter.class)
    @Setter(AccessLevel.PROTECTED)
    private Instant updatedAt;

    @Version
//    @Column(name = "version")
    @Setter(AccessLevel.PRIVATE)
    private Integer version;

    //    @PrePersist
    public void prePersist() {
        this.setCreatedAt(Instant.now());
        this.setUpdatedAt(Instant.now());
    }

    //    @PreUpdate
    public void preUpdate() {
        this.setUpdatedAt(Instant.now());
    }
}
